package com.android.basewars;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class EditAccountActivity extends Activity {
	int userID;
	EditText email, password, password2;
	Button edit;
	String passwordMD5;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.editaccountview);
		
		
		
		Bundle extras = getIntent().getExtras();
	    if(extras !=null)
	    {
	    	userID = extras.getInt("userid"); 
	    	setViews();
	    	
	    	getUser();
	    }
	    
		
	}
	
	 public void getUser() {
			Handler handler = new Handler() {
				
				public void handleMessage(Message message) {
					switch (message.what) {
					case HttpConnection.DID_START: {
						break;
					}
					case HttpConnection.DID_SUCCEED: {
						String response = (String) message.obj;
						try {
							JSONObject json_user = new JSONObject(response);
							boolean b = json_user.getBoolean("success");
							if(b){
								JSONObject user = json_user.getJSONObject("result");
								email.setText(user.getString("email"));
					 		}else{
					 			//editLogIn.setError("Inlog FOUT!");
							}
						}
						catch (JSONException e) {
							e.printStackTrace();
						}
						break;
					}
					case HttpConnection.DID_ERROR: {
						Exception e = (Exception) message.obj;
						e.printStackTrace();
						break;
					}
					}
				}
			};
			new HttpConnection(handler)
					.get("http://frankkie.nl/basewars/api/v4/User/" + userID);
		}
	
	 public String convertMD5(String s){
	        try {
	            // Create MD5 Hash
	            MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
	            digest.update(s.getBytes());
	            byte messageDigest[] = digest.digest();
	            
	            // Create Hex String
	            StringBuffer hexString = new StringBuffer();
	            for (int i=0; i<messageDigest.length; i++)
	                hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
	            return hexString.toString();
	            
	        } catch (NoSuchAlgorithmException e) {
	            e.printStackTrace();
	        }
	        return "";
		}
	 private void editAccount(){
		 
	 }
	 
	private void setViews() {
		email =	(EditText)findViewById(R.id.editTextEmailEdit);
		password = (EditText)findViewById(R.id.editTextPasswordEdit);
		password2 = (EditText)findViewById(R.id.editTextPasswordAgainEdit);
		
		edit = (Button)findViewById(R.id.buttonEditAccount);
		
		edit.setOnClickListener
    	( new View.OnClickListener(){@Override public void onClick(View v) {editAccount();}});
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.map:
	    	Intent m = new Intent(getApplicationContext(), BaseWarsActivity.class);
	 		m.putExtra("userid", userID);
	 		startActivity(m);
	 		finish();
	        return true;
	    case R.id.rankinglist:
	    	Intent rl = new Intent(getApplicationContext(), RankingActivity.class);
	 		rl.putExtra("userid", userID);
	 		startActivity(rl);
	 		finish();
	        return true;
	    case R.id.attacklog:
	    	Intent al = new Intent(getApplicationContext(), AttackLogActivity.class);
	 		al.putExtra("userid", userID);
	 		startActivity(al);
	 		finish();
	        return true;
//	    case R.id.editaccount:
//	    	Intent e = new Intent(getApplicationContext(), EditAccountActivity.class);
//	 		e.putExtra("userid", userID);
//	 		startActivity(e);
//	 		finish();
//	        return true;  
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
}
